"""
角色授权
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
import time
import logging.config
from conf import settings
from lib.common import wait_table, host_row_search,return_page

logging.config.dictConfig(settings.LOGGING_DIC)  # 导入上面定义的logging配置
logger = logging.getLogger("查询角色")  # 生成一个log实例



def role_auth():
    """
    角色授权
    """
    # 设备管理
    WebDriverWait(browser, 10, 0.5).until(EC.presence_of_element_located((
        By.XPATH, '/html/body/div[2]/div/div[2]/ul/li[3]/table/tbody/tr'
    ))).click()
    # 子iframe切换
    browser.switch_to.frame('LT_main')
    # 设备批量授权
    browser.find_element(By.XPATH, '//*[@id="sysrolemng"]').click()
    # 键入功能验证
    browser.find_element(By.XPATH, '//*[@id="roleName"]').send_keys("功能验证")

    # 等待加载
    # time.sleep(8)
    WebDriverWait(browser, 10, 0.5).until(EC.presence_of_element_located((
        By.XPATH, '//*[@id="10580"]/td[5]'
    )))

    # 查询
    browser.find_element(By.XPATH, '/html/body/div[1]/div/div/div[2]/div/table/tbody/tr[2]/td[5]/div/table').click()
    # 选中
    WebDriverWait(browser, 10, 0.5).until(EC.presence_of_element_located((
        By.XPATH, '//*[@id="47780"]'
    ))).click()
    # 编辑
    browser.find_element(By.XPATH, '/html/body/div[1]/div/div/div[3]/div/div[2]/table').click()

    # 切换iframe
    browser.switch_to.parent_frame()
    browser.switch_to.frame(4)
    # 输入ip 输入从账号 查询
    browser.find_element(By.XPATH, '//*[@id="unentityIp"]').send_keys("10.70.219.72")
    browser.find_element(By.XPATH, '//*[@id="unacctName"]').send_keys("root")
    # 等待加载
    # time.sleep(8)
    WebDriverWait(browser, 10, 0.5).until(EC.presence_of_element_located((
        By.XPATH, '//*[@id="1"]'
    )))
    # 查询
    browser.find_element(By.XPATH,
                         '/html/body/div[1]/div/div/div/div/table[2]/tbody/tr/td[1]/fieldset/table/tbody/tr[2]/td[3]/div/table/tbody/tr/td[2]').click()
    l = browser.find_element(By.XPATH, ' //*[@id="1"]').text.split(" ")
    print(l)


def no_such_role_exist(browser, wait, host_list):
    no_such_role_dict = {}

    for host in host_list:
        if host.role is None:
            return None
        if query_role(browser, wait, host.role) is None:
            no_such_role_dict[host.role] = 1
            host.is_bad = True
    return list(no_such_role_dict)


def query_role(browser, wait, role):
    pass


if __name__ == '__main__':
    print(list({"A": 1, "B": 2}))
